<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\OperatorRelation;
use yii\data\Sort;
use yii\db\Query;
use yii\data\ArrayDataProvider;

/**
 * SysItemSearch represents the model behind the search form about `backend\models\SysItem`.
 */
class OperatorRelationSearch extends OperatorRelation {
  
     
    public function rules()
    {
        return [
            [['uid'],'integer']
        ];
    }
    public function search($params)
    {
        $this->load($params);
        $query = new Query;
        
        $uid=intval($this->uid);
        if($uid>0){
            $operator=$query->from('prf_operator_relation,user_info,sys_user_designer')
            ->where("  user_info.id=prf_operator_relation.uid and sys_user_designer.uid=prf_operator_relation.uid  and prf_operator_relation.uid=:uid",array(":uid"=>$uid))
            ->select("prf_operator_relation.*,user_info.tel,sys_user_designer.name username")
            ->orderBy("id desc ")
            ->all();
        }else{
            $operator=$query->from('prf_operator_relation,user_info,sys_user_designer')
            ->where(" user_info.id=prf_operator_relation.uid  and sys_user_designer.uid=prf_operator_relation.uid")
            ->select("prf_operator_relation.*,user_info.tel,sys_user_designer.name username")
            ->orderBy("id desc ")
            ->all();
        }
        $query->andWhere([
            'uid' => $this->uid,
        ]);  
        for($i=0;$i<count($operator);$i++){
            $city_id=$operator[$i]['city_id'];
            $operator[$i]['city_id']=$this->getCityName($city_id);
            if(intval( $operator[$i]['useing'])==0){
                $operator[$i]['useing']='不使用';
            }else{
                $operator[$i]['useing']='已使用';
            }
            $city_list_str=$operator[$i]['city_list'];
            $operator[$i]['city_list']=$this->getCitylistStr($city_list_str);
        }
        $provider = new ArrayDataProvider([
            'allModels' =>$operator,
            'key' => 'uid',
            'sort' => [
                'attributes' => ['uid'],
            ],
            'pagination' => [
                'pageSize' => 10,
            ],
        ]);
        return $provider;
    }
}
 